package com.csoa.community.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import com.csoa.community.singletons.LogInfo;

public class DatabaseHelper extends SQLiteOpenHelper {
	public static final String TAG = "DatabaseHelper";

	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		create(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public static void create(SQLiteDatabase db) {
		// createLogin(db);
		createIssueGroup(db);
		createYFInfo(db);
		createWZInfo(db);
		createWZMlInfo(db);
		createPhoto(db);
		
		boolean isSuccess = createLocalSpecies(db);
		if(isSuccess) {
			
		}
	}

	// /**
	// * 登录表
	// */
	// private static boolean createLogin(SQLiteDatabase db) {
	// try {
	// db.execSQL("CREATE TABLE loginInfoTable (" + "id TEXT PRIMARY KEY, " +
	// "username TEXT," + //
	// "password TEXT ," + //
	// "autoLogin TEXT ," + //
	// "currentLogin TEXT ," + //
	// "isFrist TEXT ," + //是否是第一次登录，0为第一次，1为登录数次
	// "loginTime TEXT ," + //
	// "nickName TEXT" + //
	// ");");
	// LogInfo.LogOut(TAG + " Create Table loginInfoTable ok");
	// return true;
	// } catch (Exception e) {
	// LogInfo.LogOut(TAG + " Create Table loginInfoTable err,table exists." +
	// e.getMessage());
	// }
	// return false;
	// }

	/**
	 * 课题组表
	 */
	private static boolean createIssueGroup(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE IssueGroupTable ("
					+ "id INTEGER PRIMARY KEY, " + "unit TEXT," + // 单位
					"principal TEXT ," + // 负责人
					"writer TEXT ," + // 填报人
					"area TEXT ," + // 调查区域或流域
					"valley TEXT ," + // 流域
					"classification TEXT" + // 类别（植物，动物，微生物等，目前只是植物，为保留字段）
					");");
			LogInfo.LogOut(TAG + " Create Table IssueGroupTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG
					+ " Create Table IssueGroupTable err,table exists."
					+ e.getMessage());
		}
		return false;
	}

	/**
	 * 植物样方表
	 */
	private static boolean createYFInfo(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE YFInfoTable ("
					+ "id INTEGER PRIMARY KEY, " + "grounpId TEXT," + // 课题组id
					"surveyMethod TEXT ," + // 所属调查方法
					"gridNumber TEXT ," + // 网格编号
					"area TEXT ," + // 调查区域
					"areaPosition TEXT ," + // 调查区域
					"xiang TEXT ," + // 调查区域
					"country TEXT ," + // 调查区域
					"date TEXT ," + // 调查日期
					"quadratsNumber TEXT ," + // 样方编号
					"longitude TEXT ," + // 经度
					"latitude TEXT ," + // 纬度
					"aspect TEXT ," + // 坡向
					"slope TEXT ," + // 坡度
					"slopePosition TEXT ," + // 坡位
					"elevation TEXT ," + // 海拔
					"quadratArea TEXT ," + // 样方面积
					"smallQuadratArea TEXT ," + // 小样方面积
					"environmentFeature TEXT ," + // 环境特点
					"interference TEXT," + // 干扰因素
					"treeLayerCoverage TEXT," + // 乔木层盖度

					"guanMuCoverage TEXT ," + // 样方面积
					"caoMuCoverage TEXT ," + // 小样方面积
					"kuZhiCoverage TEXT ," + // 环境特点
					"taiXianCoverage TEXT," + // 干扰因素
					"shuGanCoverage TEXT," + // 乔木层盖度

					"community TEXT," + // 群落类型及组成
					"investigators TEXT," + // 调查人
					"imgPath TEXT," + // zhaopian
					"imgTitle TEXT," + // zhaopian
					"imgAuthor TEXT," + // zhaopian
					"videoPath TEXT," + // luxiang
					"videoTitle TEXT," + // zhaopian
					"videoAuthor TEXT," + // zhaopian
					"surfaceFeatures TEXT" + // 地表特征
					");");
			LogInfo.LogOut(TAG + " Create Table YFInfoTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table YFInfoTable err,table exists."
					+ e.getMessage());
		}
		return false;
	}

	/**
	 * 物种表(32个字段)
	 */
	private static boolean createWZInfo(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE WZInfoTable ("
					+ "id INTEGER PRIMARY KEY, yfId INTEGER,"
					+ "grounpId TEXT," + // 课题组id
					"surveyMethod TEXT ," + // 所属调查方法
					"chineseName TEXT ," + // 中文名
					"latinName TEXT ," + // 拉丁名称
					"commonName TEXT ," + // 俗名
					"characters TEXT ," + // 性状
					"quantity TEXT ," + // 数量
					"coverage TEXT ," + // 盖度
					"height TEXT ," + // 高度
					"frequentness TEXT ," + // 频度
					"DBH TEXT ," + // 胸径
					"phenology TEXT ," + // 物候
					"phenologyPosition INTEGER ," + // 物候
					"viability TEXT ," + // 生活力
					"viabilityPosition INTEGER ," + // 生活力
					"niche TEXT ," + // 生态位
					"nichePosition INTEGER ," + // 生态位
					"threatFactors TEXT ," + // 受威胁因素
					"specimenNumber TEXT ," + // 标本号
					"photoNumber TEXT," + // 照片号
					"smallQuadratNumber TEXT," + // 小样方号
					"importantLevel TEXT," + // 重要级别
					"level TEXT," + // 层次
					"levelPosition INTEGER," + // 层次
					"formations TEXT," + // 群系
					"distributionType TEXT," + // 分布类型
					"distributionTypePosition INTEGER," + // 分布类型
					"imgPath TEXT," + // zhaopian
					"imgTitle TEXT," + // zhaopian
					"imgAuthor TEXT," + 
					"videoPath TEXT," + // luxiang
					"videoTitle TEXT," + // zhaopian
					"videoAuthor TEXT," + // zhaopian
					"distributionArea TEXT" + // 分布区域
					");");
			LogInfo.LogOut(TAG + " Create Table WZInfoTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table WZInfoTable err,table exists."
					+ e.getMessage());
		}
		return false;
	}

	/**
	 * 物种名录表30项
	 */
	private static boolean createWZMlInfo(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE WZMLInfoTable ("
					+ "id INTEGER PRIMARY KEY, " + "grounpId TEXT," + // 课题组id
					"surveyMethod TEXT ," + // 所属调查方法
					"surveyArea TEXT ," + // 调查区域
					"surveyAreaPosition INTEGER ," + // 调查区域
					"chineseName TEXT ," + // 中文名
					"latinName TEXT ," + // 拉丁名称
					"commonName TEXT ," + // 俗名
					"characters INTEGER ," + // 习性
					"distributionTypePosition INTEGER ," + // 习性
					"distributionArea TEXT ," + // 分布区域
					"environmentFeature TEXT ," + // 生活环境
					"elevationBegin TEXT ," + // 海拔
					"elevationEnd TEXT ," + // 海拔
					"level INTEGER ," + // 习性
					"niche INTEGER ," + // 习性
					"isOrigin INTEGER ," + // 是否原产
					"endemism INTEGER ," + // 特有性
					"degrees INTEGER ," + // 多度
					"use TEXT ," + // 用途
					"nationalUse TEXT ," + // 民族用途
					"threatStates INTEGER ," + // 受威胁现状
					"threatFactors TEXT ," + // 受威胁因素
					"saveGradeInCountry INTEGER ," + // 保护等级
					"saveGradeInPrivince INTEGER ," + // 保护等级
					"saveGradeInCITES INTEGER ," + // 保护等级
					"saveGradeInRed INTEGER ," + // 保护等级
					"imgPath TEXT," + // zhaopian
					"imgTitle TEXT," + // zhaopian
					"imgAuthor TEXT," + 
					"videoPath TEXT," + // luxiang
					"videoTitle TEXT," + // zhaopian
					"videoAuthor TEXT," + // zhaopian
					"saveStates INTEGER" + // 保护状态

					");");
			LogInfo.LogOut(TAG + " Create Table WZMlInfoTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG
					+ " Create Table WZMlInfoTable err,table exists."
					+ e.getMessage());
		}
		return false;
	}

	/**
	 * 物种本底库表
	 */
	private static boolean createLocalSpecies(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE T_Species_PlantCodeDic ("
					+ "Id TEXT , " + "Type TEXT ," + // 类型
					"SName TEXT ," + //拉丁名称
					"SpNumber TEXT ," + // 
					"Family TEXT ," + // 科
					"Genus TEXT ," + //属
					"Sp1 TEXT ," + // 种
					"Author1 TEXT ," + // 作者
					"fullname TEXT ," + // 全称
					"Chinese TEXT ," + // 中文名
					"occurrence TEXT ," + // 发源地
					"originarea TEXT ," + // 原产地
					"cultivated TEXT ," + // 引进来源
					"endemism TEXT ," + // 物种属性
					"Country TEXT ," + // 分布国家
					"Disprovin TEXT " + //分布省份

					");");
			LogInfo.LogOut(TAG + " Create Table T_Species_PlantCodeDic ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG
					+ " Create Table T_Species_PlantCodeDic err,table exists."
					+ e.getMessage());
		}
		return false;
	}

	/**
	 * 植物图片表
	 */
	private static boolean createPhoto(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE photoTable (" + "id INTEGER PRIMARY KEY, " + // 照片路径
					"ownerId TEXT ," + // 所属调查方法（样方法，物种，为物种名录）
					"type TEXT ," + // （0为样方法，1为物种，2为物种名录）
					"mediaType TEXT," + // (0为照片，1为录像)
					"path TEXT," + // 照片路径
					"title TEXT ," + // 照片标题
					"photoNumber TEXT ," + // 照片号
					"author TEXT ," + // 作者或来源
					"remarks TEXT , " + // 备注
					"time TEXT" + // 作者或来源
					
					");");
			LogInfo.LogOut(TAG + " Create Table photoTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table photoTable err,table exists."
					+ e.getMessage());
		}
		return false;
	}

	// /**
	// * 创建UUID表 通过用户uuid查询对应文章aaid(获取首页信息时，用于从数据库中查询相应用户的文章)
	// *
	// * @return
	// */
	// private static boolean createUUID(SQLiteDatabase db) {
	// try {
	// db.execSQL("CREATE TABLE UUIDTable (" + "uuID TEXT," + //
	// "aaID TEXT PRIMARY KEY" + //
	// ");");
	// LogInfo.LogOut(TAG + " Create Table UUIDTable ok");
	// return true;
	// } catch (Exception e) {
	// LogInfo.LogOut(TAG + " Create Table UUIDTable err,table exists." +
	// e.getMessage());
	// }
	// return false;
	// }

}
